//
//  HTMLViewController.swift
//  WangPu
//
//  Created by xiaozhi on 15/10/30.
//  Copyright © 2015年 zhixiao. All rights reserved.
//

import UIKit

class HTMLViewController: BaseViewController {
    var bridge:WebViewJavascriptBridge?
    var bannerObj:BannerObj?
    override func viewDidLoad() {
        super.viewDidLoad()
//        self.title = "HTML5"
        let backItem = UIBarButtonItem(image: UIImage(named: "back_icon"), style:UIBarButtonItemStyle.Plain, target:self, action:"goBack")
        backItem.tintColor = UIColor.whiteColor()
        self.navigationItem.leftBarButtonItem = backItem
        self.automaticallyAdjustsScrollViewInsets = false
        self.view.backgroundColor = UIColor.whiteColor()
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    override func viewWillAppear(animated: Bool){
        super.viewWillAppear(animated)
        navigationController?.navigationBarHidden = false
        if bridge != nil{
            return
        }
        let webview = UIWebView(frame: CGRectMake(0, 64, view.bounds.size.width, view.bounds.size.height-64))
        view.addSubview(webview)
        WebViewJavascriptBridge.enableLogging()
       bridge =  WebViewJavascriptBridge(forWebView: webview, webViewDelegate: self){ (data, responseCallback) -> Void in
            print(data)
            responseCallback("adfad")
        }
        bridge?.registerHandler("testObjcCallback", handler: { (data, responseCallback) -> Void in
            print(data)
            responseCallback("Response from testObjcCallback")
            let dic = data as! NSDictionary
            let ctl = GoodsDetailViewController()
            ctl.goodsID = dic["id"] as? String
            ctl.hidesBottomBarWhenPushed = true
            self.navigationController?.pushViewController(ctl, animated: true)

        })
        if self.bannerObj?.link != nil{
            let url = NSURL(string: self.bannerObj!.link!)
            let request = NSMutableURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringCacheData, timeoutInterval: 30.0)
            webview.loadRequest(request)
 
        }
        self.showProcess("加载中...")

    }
    func  goBack(){
        self.navigationController?.popViewControllerAnimated(true)
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
extension HTMLViewController:UIWebViewDelegate{
    func webViewDidStartLoad(webView: UIWebView){
        
    }
    func webViewDidFinishLoad(webView: UIWebView){
        self.hiddenProcessView()
    }
    func webView(webView: UIWebView, didFailLoadWithError error: NSError?){
        self.hiddenProcessView()
        self.showTips("加载失败")
    }
}